package main.java.controller;


import main.java.pojo.Good;
import main.java.pojo.OutGood;
import main.java.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

@Controller
public class GoodController {

    @Autowired
    private GoodService goodService;

    @RequestMapping("/goodlist")
    public String goodlist(Model model){
        Collection<Good> goods=goodService.queryGood();

        model.addAttribute("goods",goods);
        return "good_list";
    }

    @RequestMapping("/togood_add")
    public String togood_add(){
        return "admin/good_add";
    }

    @RequestMapping("/toingood_list")
    public String toingood_list(Model model){
        Collection<Good> goods=goodService.queryinGood();
        model.addAttribute("goods",goods);
        return "ingood_list";
    }

    @RequestMapping("/tooutgood_list")
    public String tooutgood_list(Model model){
        Collection<OutGood> outGoods=goodService.queryoutGood();
        model.addAttribute("goods",outGoods);
        return "outgood_list";
    }

    @PostMapping("/AddGood")
    public String AddGood(Good good){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date data=new Date();
        String date= sdf.format(data);
        good.setIndate(date);
        System.out.println(good.toString());
        goodService.addGood(good);
        goodService.inGood(good);
        return "redirect:/goodlist";
    }

    @RequestMapping("/toupdateGood{id}")
    public String updateGood(@PathVariable("id") Integer id,Model model){
        Good good=goodService.queryGoodById(id);
        System.out.println("111111111"+good.toString());
        model.addAttribute("good",good);
        return "admin/good_update";
    }

    @RequestMapping("/updateGood")
    public String updateGood(Good good){
        goodService.updateGood(good);
        return "redirect:/goodlist";
    }
    @RequestMapping("/deleteGood{id}")
    public String deleteGood(@PathVariable("id") Integer id,Model model){
        Good good=goodService.queryGoodById(id);
        model.addAttribute("good",good);
        return "admin/good_out";
    }

    @RequestMapping("/outGood")
    public String outGood(OutGood outGood)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date data=new Date();
        String date= sdf.format(data);
        outGood.setOutdate(date);
        goodService.outGood(outGood);
        goodService.deleteGood(outGood.getId());
        return "redirect:/goodlist";
    }
}
